package com.niiwoo.civet.scheduler.trigger;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.rpc.RpcContext;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.DataReportChinaJrbRequestDTO;
import com.niiwoo.civet.trade.dto.request.thirdplateformdata.DataReportJrbRequestDTO;
import com.niiwoo.civet.trade.service.statisticsinfo.StatisticsInfoDubboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.concurrent.CompletableFuture;


/**
 * 统计数据相关定时任务
 *
 * @author wuke
 * @date 2017/12/29 17:28
 */
@Slf4j
@Component
public class StatisticsInfoJobTrigger {

    @Reference(version = "1.0.0")
    private StatisticsInfoDubboService statisticsInfoDubboService;

    @Scheduled(cron = "${platform.update_statistics_info}")
    public void updateStatisticsInfoForJob() {
        log.info("updateStatisticsInfoForJob start");
        CompletableFuture.runAsync(() -> statisticsInfoDubboService.updateStatisticsInfoForJob());
        log.info("updateStatisticsInfoForJob end");
    }


    @Scheduled(cron = "${platform.scheduled-report-data-jrb}")
    public void scheduledReportDataJrb() {
        log.info("scheduledReportDataJrb start");
        DataReportJrbRequestDTO dto = new DataReportJrbRequestDTO();
        dto.setIsAutoUpload(true);
        RpcContext.getContext().asyncCall((Runnable) () -> statisticsInfoDubboService.reportDataToJrb(dto));
        log.info("scheduledReportDataJrb end");
    }


    @Scheduled(cron = "${platform.scheduled-china-report-data-jrb}")
    public void scheduledChinaReportDataJrb() {
        log.info("scheduledChinaReportDataJrb start");
        DataReportChinaJrbRequestDTO dto = new DataReportChinaJrbRequestDTO();
        dto.setIsAutoUpload(true);
        dto.setIsMonthReport(true);
        dto.setReportDate("");
        RpcContext.getContext().asyncCall(() -> {
            try {
                statisticsInfoDubboService.reportDataToChinaJrb(dto);
            } catch (IOException e) {
                log.error("scheduledChinaReportDataJrb error",e);
            }
        });
        log.info("scheduledChinaReportDataJrb end");
    }

}